import java.awt.Button;
import java.awt.Color;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;

public class Solitaire {
	private Frame window;
	private Card movingCard;
	private CardPile previousPile;

	static public CardPile allPiles[];
	static public DeckPile deckPile;
	static public DiscardPile discardPile;
	static public SuitPile suitPile[];
	static public TablePile tablePile[];

	public static void main(String[] args) {
		Solitaire solitare = new Solitaire();
	}

	public Solitaire() {
		window = new SolitaireFrame();
		init();
		window.show();
	}

	private void init() {
		allPiles = new CardPile[13];
		suitPile = new SuitPile[4];
		tablePile = new TablePile[7];

		allPiles[0] = deckPile = new DeckPile(670, 30);
		allPiles[1] = discardPile = new DiscardPile(570, 30);
		for (int i = 0; i < 4; i++)
			allPiles[2 + i] = suitPile[i] = new SuitPile(15 + (Card.width + 35)
					* i, 30);
		for (int i = 0; i < 7; i++) {
			allPiles[6 + i] = tablePile[i] = new TablePile(15
					+ (Card.width + 40) * i, Card.height + 35, i + 1);
		}
	}

	class SolitaireFrame extends JFrame {

		private class RestartButtonListener implements ActionListener {
			@Override
			public void actionPerformed(ActionEvent e) {
				init();
				window.repaint();
			}
		}

		private class MouseKeeper implements MouseListener, MouseMotionListener {
			public void mousePressed(MouseEvent e) {
				int x = e.getX();
				int y = e.getY();
				for (int i = 0; i < 13; i++)
					if (allPiles[i].includes(x, y)) {
						if (!allPiles[i].isEmpty()) {
							allPiles[i].select();
							previousPile = allPiles[i];
							movingCard = allPiles[i].pop();
						}
					}
				repaint();
			}

			@Override
			public void mouseDragged(MouseEvent e) {

			}

			@Override
			public void mouseReleased(MouseEvent e) {
				int x = e.getX();
				int y = e.getY();
				if (movingCard != null) {
					for (int i = 0; i < 13; i++)
						if (allPiles[i].includes(x, y)) {
							if (allPiles[i].canTake(movingCard)) {
								allPiles[i].addCard(movingCard);
							} else {
								previousPile.addCard(movingCard);
							}
						}
				}
				repaint();

			}

			@Override
			public void mouseClicked(MouseEvent e) {
			}

			@Override
			public void mouseEntered(MouseEvent e) {
			}

			@Override
			public void mouseExited(MouseEvent e) {
			}

			@Override
			public void mouseMoved(MouseEvent e) {
			}
		}

		public SolitaireFrame() {
			this.setLocation(250, 100);
			this.getContentPane().setBackground(new Color(0, 112, 26));
			this.setDefaultCloseOperation(EXIT_ON_CLOSE);
			setSize(800, 600);
			setTitle("Solitaire Game");
			addMouseListener(new MouseKeeper());
			Button restartButton = new Button("new Game");
			restartButton.addActionListener(new RestartButtonListener());
			add("South", restartButton);

		}

		public void paint(Graphics g) {
			super.paint(g);
			for (int i = 0; i < 13; i++) {
				allPiles[i].display((Component) this, g);
			}
		}
	}
}